package com.eobard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eobard.entity.Department;
import com.eobard.mapper.DepartmentMapper;
import com.eobard.mapper.UserMapper;
import com.eobard.service.DepartmentService;
import com.eobard.utils.DepartmentTree;
import com.eobard.vo.DepartmentQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Eobard Thawne
 * @since 2022-07-31
 */
@Service
@Transactional
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Resource
    private UserMapper userMapper;

    /**
     * 查询部门列表
     */
    @Override
    public List<Department> findDepartmentList(DepartmentQueryVo departmentQueryVo) {
        //查询所有部门列表
        List<Department> departmentList = findDepartmentByDepartmentName(departmentQueryVo);

        List<Department> departmentTree=new ArrayList();
        //生成部门树
        if(!departmentList.isEmpty()){
            departmentTree=DepartmentTree.makeDepartmentTree(departmentList,departmentList.stream().mapToLong(Department::getPid).min().getAsLong());
        }
        return departmentTree;
    }

    /**
     * 根据部门名称模糊查询部门
     */
    @Override
    public List<Department> findDepartmentByDepartmentName(DepartmentQueryVo departmentQueryVo) {
        //创建条件构造器
        QueryWrapper<Department> wrapper=new QueryWrapper<>();
        //模糊查询部门名称
        wrapper.like(!ObjectUtils.isEmpty(departmentQueryVo.getDepartmentName()),"department_name",departmentQueryVo.getDepartmentName());
        //根据id排序
        wrapper.orderByAsc("order_num");
        //查询部门列表
        List<Department> departmentList = baseMapper.selectList(wrapper);
        return departmentList;
    }


    /**
     * 查询上级部门列表
     */
    @Override
    public List<Department> findParentDepartment() {
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
        //排序
        queryWrapper.orderByAsc("order_num");
        //查询部门列表
        List<Department> departmentList = baseMapper.selectList(queryWrapper);
        //创建部门对象
        Department department = new Department();
        department.setId(0L);
        department.setDepartmentName("顶级部门");
        department.setPid(-1L);
        departmentList.add(department);
        //生成部门树
        return DepartmentTree.makeDepartmentTree(departmentList,-1L);
    }


    /**
     * 判断当前部门下是否有子部门
     */
    @Override
    public boolean hasChildrenOfDepartment(Long id) {
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
        //根据要删除的部门id来查询 其它部门的pid是否等于它:若其它的pid=id说明有子部门,则不能删除
        queryWrapper.eq("pid",id);
        //如果数量大于0，表示存在子部门
        if(baseMapper.selectCount(queryWrapper)>0){
            return true;
        }
        return false;
    }

    /**
     * 查询部门下是否存在用户
     */
    @Override
    public boolean hasUserOfDepartment(Long id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        //查询要删除的部门id是否等于用户的department_id
        queryWrapper.eq("department_id",id);
        if(userMapper.selectCount(queryWrapper)>0){
            return true;
        }
        return false;
    }

}
